import java.util.StringTokenizer;
import java.util.Vector;

class P196_39643 {

	private static Vector formulas;

	public static void main(String[] args) {
		int cases = Integer.parseInt(readLn());

		for (int k = 0; k < cases; k++) {
			String line = readLn();
			while ("".equals(line)) {
				line = readLn();
			}
			StringTokenizer st = new StringTokenizer(line);
			int cols = Integer.parseInt(st.nextToken());
			int rows = Integer.parseInt(st.nextToken());
			process(cols, rows);
		}

	}

	private static void process(int cols, int rows) {

		String[][] ss = new String[rows][cols];

		formulas = new Vector();

		int i = 0, j = 0;

		getmatrix(cols, rows,  ss, i, j);

		compute(ss);

		print(ss);

	}

	private static void getmatrix(int cols, int rows, String[][] ss, int i, int j) {
		while (true) {
			String line = readLn();
//			System.out.println(">> " + line);
			StringTokenizer st = new StringTokenizer(line);
			while (st.hasMoreTokens()) {
//				System.out.println("i=" + i);
//				System.out.println("j=" + j);
				
				String x = st.nextToken();
				ss[i][j] = x;
				if (x.startsWith("=")) {
					Formula formula = new Formula(x, i, j);
					formulas.addElement(formula);
				}
				j++;
				
				
				
				if (j == cols) {
					j = 0;
					i++;
				}
				
				if (j == 0 && i== rows) return;
			}
		}
	}

	private static void compute(String[][] ss) {
		Vector formulasAux = new Vector(formulas.size());
		while (!formulas.isEmpty() || !formulasAux.isEmpty()) {
			if (formulas.isEmpty()) {
				formulas = formulasAux;
				formulasAux = new Vector(formulas.size());
			}
			Formula formula = (Formula) formulas.elementAt(formulas.size()-1);
			formulas.removeElement(formula);
			String value = getValue(formula, ss);
			if (value != null) {
				ss[formula.row][formula.col] = value;
			} else {
				formulasAux.addElement(formula);
			}
		}
	}

	private static String getValue(Formula formula, String[][] ss) {
		StringTokenizer st = new StringTokenizer(formula.formula.substring(1),
				"+");
		int value = 0;

		while (st.hasMoreTokens()) {
			String token = st.nextToken();
			int[] coord = convertRowCol(token);
			String valueSS = ss[coord[0]][coord[1]];
			if (valueSS.startsWith("="))
				return null;

			value += Integer.parseInt(valueSS);
		}

		return value + "";
	}

	public static int convertCol(String colString) {
		int col = 0;
		for (int k = colString.length() - 1; k >= 0; k--) {
			char c = colString.charAt(colString.length() -1  - k);
			int n = (c - 'A') + 1;
			col += n * (int) Math.pow(26, k);
		}
		
		return col - 1;
	}

	public static int[] convertRowCol(String string) {
		int[] coord = new int[2];
		String stringPart = null;
		for (int k = 0; k < string.length(); k++) {
			char c = string.charAt(k);
			if (c >= '0' && c <= '9') {
				stringPart = string.substring(0, k);
				break;
			}
		}

		coord[1] = convertCol(stringPart);

		coord[0] = Integer.parseInt(string.substring(stringPart.length())) - 1;

		return coord;
	}

	private static void print(String[][] ss) {
		StringBuffer ssString = new StringBuffer();
		for (int k = 0; k < ss.length; k++) {
			StringBuffer row = new StringBuffer();
			for (int j = 0; j < ss[k].length; j++) {
				row.append(ss[k][j] + " ");
			}
			ssString.append(row.toString().trim() + "\n");
		}

		System.out.print(ssString.toString());

	}


	static String readLn() {
		String newLine = System.getProperty("line.separator");
		StringBuffer buffer = new StringBuffer();
		int car = -1;
		try {
			car = System.in.read();
			while ((car > 0) && (car != newLine.charAt(0))) {
				buffer.append((char) car);
				car = System.in.read();
			}
			if (car == newLine.charAt(0))
				System.in.skip(newLine.length() - 1);
		} catch (java.io.IOException e) {
			return (null);
		}
		if ((car < 0) && (buffer.length() == 0))
			return (null);
		return (buffer.toString()).trim();
	}

}

class Formula {

	int row;

	int col;

	String formula;

	Formula(String formula, int row, int col) {
		this.row = row;
		this.col = col;
		this.formula = formula;
	}

}
